using System.Collections;
using System.Collections.Generic;
using TripleMatch;
using UnityEditor;
using UnityEngine;

public class ConfigImportTool 
{
    [MenuItem("配置导入/导入装饰物数据")]
    public static void ImportDecorationInfo()
    {
        Debug.Log("===== ImportDecorationInfo ===== Start");

        string l_dataPath = "Assets/Test2/Datas/DecorationsData.asset";
        Debug.Log("l_dataPath = " + l_dataPath);

        DecorationsData decorationsData= AssetDatabase.LoadAssetAtPath<DecorationsData>(l_dataPath);
        if (decorationsData == null)
        {
            Debug.LogError("decorationsData == null");
            return;
        }

        string cfgPath = System.Environment.CurrentDirectory + "/Assets/Test2/Datas/DecorationsData.csv";
        Debug.Log("cfgPath = " + cfgPath);

        List<DecorationConfigData> infoDatas = Util.LoadCsvFileList<DecorationConfigData>(cfgPath, 2);
        if (infoDatas == null || infoDatas.Count == 0)
        {
            Debug.LogError("infoDatas == null || infoDatas.Count == 0 cfgPath = " + cfgPath);
            return;
        }

        //获得装饰物配置
        List<DecorationInfoData> decorationInfoDatas = new List<DecorationInfoData>();

        //获得装饰组别配置
        List<StyleGroupData> styleGroupDatas = new List<StyleGroupData>();

        foreach (var item in infoDatas)
        {
            DecorationInfoData decorationInfoData = new DecorationInfoData();
            decorationInfoData.id = item.ID;
            decorationInfoData.decorationType = item.decorationType;
            decorationInfoData.spriteName = item.spriteName;
            decorationInfoData.displayOrder = item.displayOrder;
            decorationInfoData.qualityIndex = item.qualityIndex;
            decorationInfoData.styleGroup = item.styleGroup;
            decorationInfoData.description1 = item.description1;
            decorationInfoData.description2 = item.description3;
            decorationInfoDatas.Add(decorationInfoData);

            bool containedGroup = false;
            for (int i = 0; i < styleGroupDatas.Count; i++)
            {
                if (styleGroupDatas[i].id == decorationInfoData.styleGroup)
                {
                    containedGroup = true;
                    styleGroupDatas[i].groupCount++;
                    styleGroupDatas[i].groupItems.Add(decorationInfoData.id);
                }
            }

            if (!containedGroup)
            {
                StyleGroupData styleGroupData = new StyleGroupData();
                styleGroupData.id = decorationInfoData.styleGroup;
                styleGroupData.groupCount = 1;
                styleGroupData.groupItems = new List<int>() { decorationInfoData.id };
                styleGroupData.groupDescription = decorationInfoData.description2;
                styleGroupDatas.Add(styleGroupData);
            }
        }

        decorationsData.infoDatas = decorationInfoDatas.ToArray();
        decorationsData.groupDatas = styleGroupDatas.ToArray();

        EditorUtility.SetDirty(decorationsData);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.Log("===== ImportDecorationInfo ===== Finish");
    }

    [System.Serializable]
    public class DecorationConfigData
    {
        /// <summary>
        /// 装饰物唯一标识
        /// </summary>
        public int ID;
        /// <summary>
        /// 装饰物种类编号
        /// </summary>
        public int decorationType;
        /// <summary>
        /// 装饰物资源名称
        /// </summary>
        public string spriteName;
        /// <summary>
        /// 装饰物展示优先级
        /// </summary>
        public int displayOrder;
        /// <summary>
        /// 装饰物品质
        /// </summary>
        public int qualityIndex;
        /// <summary>
        /// 装饰物所属组别
        /// </summary>
        public int styleGroup;
        /// <summary>
        /// 描述一：装饰物展示名称
        /// </summary>
        public string description1;
        /// <summary>
        /// 描述二：装饰物组别介绍
        /// </summary>
        public string description2;
        /// <summary>
        /// 描述三：装饰物简介
        /// </summary>
        public string description3;
    }
}
